﻿// /////////////////////////////////////////////////////////////////////
// 
//  Codeplex JSAnalyse
//  Copyright (c) 2011
//  JSAnalyse.Core   JavascriptParser.cs
// 
// /////////////////////////////////////////////////////////////////////
using Jint;
using Jint.Expressions;

namespace JSAnalyse.Core.Parser
{
    internal class JavascriptParser
    {
        #region Private variables

        private Program _program;
        private StatementVisitor _visitor = new StatementVisitor();

        #endregion

        #region Public methods

        /// <summary>
        /// Parses the specified javascript content.
        /// </summary>
        /// <param name="jsFileInfo">js file information</param>
        /// <returns>information for parsed file</returns>
        public ParserResult Parse(JavascriptFileInfo jsFileInfo)
        {
            return Parse(jsFileInfo.FileContent, jsFileInfo.File);
        }

        /// <summary>
        /// Parses the specified javascript content.
        /// </summary>
        /// <param name="javascriptContent">Content of the javascript.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>information for parsed file</returns>
        /// <remarks></remarks>
        private ParserResult Parse(string javascriptContent, string fileName)
        {
            var result = new ParserResult(fileName);
            // Compile script
            _program = JintEngine.Compile(javascriptContent, true);

            if (_program != null)
            {
                // Start visitor
                _program.Accept(_visitor);
            }

            // Get Member expressions
            result.CallDependencies = _visitor.MemberExpressions;
            result.Declarations = _visitor.Declarations;
            return result;
        }

        #endregion
    }
}
